#include "keyworddialog.h"
#include "ui_keyworddialog.h"
#include "../CheckInfor/DataBase/keywordlib.h"
#include <QInputDialog>

KeyWordDialog::KeyWordDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::KeyWordDialog)
{
    ui->setupUi(this);
    InitUi();
}

KeyWordDialog::~KeyWordDialog()
{
    delete ui;
}


void KeyWordDialog::InitUi()
{
    //设置单词检查策略

    ui->wordmethod_box->addItem("包含任意关键词",QVariant(int(Yzty::WordCheckMethod::ANYWORD)));
    ui->wordmethod_box->addItem("包含全部关键词",QVariant(int(Yzty::WordCheckMethod::ALLWORD)));
    ui->wordmethod_box->addItem("不包含任意关键词",QVariant(int(Yzty::WordCheckMethod::NOANYWORD)));

    //设置右键菜单
    ui->Words_Edit->setContextMenuPolicy(Qt::CustomContextMenu);
    connect(ui->Words_Edit,&QPlainTextEdit::customContextMenuRequested,this,&KeyWordDialog::RightMenuRequest);

    this->_menu=new QMenu(ui->Words_Edit);
    _wordadd=new QAction("添加单词",this->_menu);
    _worddelete=new QAction("删除单词",this->_menu);
    _wordmodify=new QAction("修改单词",this->_menu);

    _menu->addAction(_wordadd);
    _menu->addAction(_worddelete);
    _menu->addAction(_wordmodify);
    connect(_wordadd,&QAction::triggered,this,&KeyWordDialog::WordAdd);
    connect(_worddelete,&QAction::triggered,this,&KeyWordDialog::WordDelte);
    connect(_wordmodify,&QAction::triggered,this,&KeyWordDialog::WordModify);

    ui->WordType_list->setSelectionMode(QAbstractItemView::ExtendedSelection);  //选着模式　按住ctrl可以多选

    connect(ui->word_load,&QPushButton::clicked,this,&KeyWordDialog::LoadLib_Button_CLick); //加载关键词信息
    connect(ui->Word_cancle,&QPushButton::clicked,[this](){
        this->reject();
    });

    connect(ui->define_button,&QPushButton::clicked,this,&KeyWordDialog::AddCustomize_Button_Click);    //添加自定库

    this->_custdialog=new CustomizeDialog(this);
}

int KeyWordDialog::exec()
{
    Update();
    return QDialog::exec();
}

void KeyWordDialog::Update()
{
    ui->WordType_list->clear();
    ui->Words_Edit->clear();

    auto libnames=KeyWordLib::GetInstance()->GetLibNameList();

    for(Yzty::WordLibCode code:libnames.keys())
    {
        QListWidgetItem* item=new QListWidgetItem(ui->WordType_list);
        item->setText(libnames[code]);
        item->setData(Qt::UserRole,QVariant(qint64(code)));
        ui->WordType_list->addItem(item);
    }
    connect(ui->WordType_list,&QListWidget::clicked,this,&KeyWordDialog::ListItem_Click);

    this->_keywordlimit._marks=0;
    this->_keywordlimit._methods.clear();
}

void KeyWordDialog::RightMenuRequest(const QPoint&)
{
    QModelIndex index=ui->WordType_list->currentIndex();
    if(!index.isValid())
    {
        _wordadd->setEnabled(false);
        _worddelete->setEnabled(false);
        _wordmodify->setEnabled(false);
    }else
    {
        _wordadd->setEnabled(true);
        _worddelete->setEnabled(true);
        _wordmodify->setEnabled(true);
    }
    this->_menu->exec(cursor().pos());
}

void KeyWordDialog::ListItem_Click(const QModelIndex& index)
{
    if(!index.isValid())
        return;
    ui->Words_Edit->clear();
    Yzty::WordLibCode code=index.data(Qt::UserRole).toLongLong();
    const QVector<QString>& words=KeyWordLib::GetInstance()->GetWordList(code);

    QString text=QStringList(words.toList()).join('|');
    ui->Words_Edit->setPlainText(text);
}

void KeyWordDialog::AddCustomize_Button_Click()
{
    int ret=this->_custdialog->exec();

    if(ret==1)
    {
        auto temp=_custdialog->GetWordLibInfor();
        KeyWordLib::GetInstance()->AddKeyWordLib(temp.first,temp.second);
        KeyWordDialog::Update();
    }

}
void KeyWordDialog::SetDefaultKeywordLib()
{
    QList<QListWidgetItem*> items=ui->WordType_list->selectedItems();
        _keywordlimit._marks|=items[0]->data(Qt::UserRole).toLongLong();
        _keywordlimit._methods[items[0]->data(Qt::UserRole).toLongLong()]=Yzty::WordCheckMethod(ui->wordmethod_box->currentData().toInt());
    accept();
}
void KeyWordDialog::LoadLib_Button_CLick()
{
    QList<QListWidgetItem*> items=ui->WordType_list->selectedItems();
    for(QListWidgetItem* item:items)
    {
        _keywordlimit._marks|=item->data(Qt::UserRole).toLongLong();
        _keywordlimit._methods[item->data(Qt::UserRole).toLongLong()]=Yzty::WordCheckMethod(ui->wordmethod_box->currentData().toInt());
    }
    accept();
}

Yzty::KeyWordLimit KeyWordDialog::GetKeyWordInfor()
{
    return this->_keywordlimit;
}

void KeyWordDialog::WordDelte()
{

}

void KeyWordDialog::WordAdd()
{

}

void KeyWordDialog::WordModify()
{

}


